package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbLabel;
import com.baike.feign.TbLessonFeign;
import com.baike.service.impl.TbLabelServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author lbs
 * @since 2021-10-29
 */
@RestController
@RequestMapping("labelController")
@Api("标签管理")
public class TbLabelController {

    @Autowired
    private TbLabelServiceImpl labelService;
    @Autowired
    private TbLessonFeign lessonFeign;


    @PostMapping
    @ApiOperation("标签增加功能")
    @ApiImplicitParam(value = "标签实体数据",name = "label",required = true,paramType = "body")
    public Result<ConstantEnum> postLable(@RequestBody TbLabel label){
        // 参数判断
        if(Objects.nonNull(label)){
            //数据增加
            boolean b = labelService.save(label);
            if(b){
                return Result.build(ConstantEnum.SUCCESS);
            }
            return Result.build(ConstantEnum.ERROR);
        }
        // 参数为空
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @DeleteMapping
    @ApiOperation("标签删除功能")
    @ApiImplicitParam(value = "标签实体id集合",name = "ids",required = true,paramType = "query")
    public Result<ConstantEnum> delLable(@RequestParam("ids")Integer[] ids){

        if(Objects.nonNull(ids)){
            boolean b = labelService.removeByIds(Arrays.asList(ids));

            if(b){
                // 删除关联表信息
                String idsStr = "";
                for (int i = 0 ; i < ids.length;i++){
                    idsStr += "," + ids[i];
                }
                Result<ConstantEnum> result = lessonFeign.delLessonLabelByLabelStr(idsStr.substring(1));
                // 设置调用关联表成功与否信息
                String message = "";
                if(result.getCode().equals(2000)){
                    message = "删除关联表成功";
                }else {
                    message = "删除关联表失败";
                }

                return Result.build(ConstantEnum.SUCCESS).setResult(message);
            }
            return Result.build(ConstantEnum.ERROR);

        }
        // 参数为空
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);

    }

    @PutMapping
    @ApiOperation("标签修改功能")
    @ApiImplicitParam(value = "标签修改实体类数据",name = "label",required = true,paramType = "body")
    public Result<ConstantEnum> putLable(@RequestBody TbLabel label){

        if(Objects.nonNull(label)){
            boolean b = labelService.updateById(label);
            if(b){
                return Result.build(ConstantEnum.SUCCESS);
            }
            return Result.build(ConstantEnum.ERROR);
        }
        // 参数为空
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @PostMapping("findPage")
    @ApiOperation("标签搜索功能")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页数",name = "pageNumber",required = false,paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",required = false,paramType = "query"),
            @ApiImplicitParam(value = "标签关键字",name = "labelName",required = false,paramType = "query")
    })
    public Result<TbLabel> findPage(@RequestParam(value = "pageNumber",defaultValue = "0")Integer pageNumber,
                                    @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                                    @RequestParam(value = "labelName",defaultValue = "")String labelName
                                    ){

        // 设置分页条数 默认0页 5 条
        Page<TbLabel> page = new Page<>(pageNumber,pageSize); // 如果不要总记录时 在这里写个 false
        //根据传来的关键字 不为空就作为条件
        LambdaQueryWrapper<TbLabel> wrapper = Wrappers.<TbLabel>lambdaQuery().like(StringUtils.isNotEmpty(labelName),TbLabel::getLabelName, labelName);
        IPage<TbLabel> userIPage = labelService.page(page,wrapper);
        List<TbLabel> labelList = userIPage.getRecords();
        if(labelList.size() > 0){
            return Result.build(ConstantEnum.SUCCESS).setResult(labelList);
        }

        return Result.build(ConstantEnum.NULL_DATA);
    }

}
